package com.woniuxy.project115.kaer.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.project115.kaer.user.entity.UserInfo;
import com.woniuxy.project115.kaer.user.entity.UserVehicle;
import com.woniuxy.project115.kaer.user.mapper.UserInfoMapper;
import com.woniuxy.project115.kaer.user.mapper.UserVehicleMapper;
import com.woniuxy.project115.kaer.user.param.AddVehicleParam;
import com.woniuxy.project115.kaer.user.service.UserVehicleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 车辆表 服务实现类
 * </p>
 *
 * @author fyy
 * @since 2025年02月17日
 */
@Service
public class UserVehicleServiceImpl extends ServiceImpl<UserVehicleMapper, UserVehicle> implements UserVehicleService {
    @Resource
    private UserVehicleMapper userVehicleMapper;
    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public void addUserVehicle(AddVehicleParam param) throws Exception {
        //查询用户是否存在
        final QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",param.getUserId());
        UserInfo user = userInfoMapper.selectOne(queryWrapper);
        if(user == null){
            throw new Exception("用户不存在");
        }
        if(user.getState()!=0){
            throw new Exception("用户状态异常");
        }
        userVehicleMapper.insert(BeanUtil.toBean(param, UserVehicle.class));
    }
}
